package com.common.android.utils.audio;

import java.io.File;
import java.io.IOException;

import com.common.android.utils.audio.AudioPlayer.onPlayFinishListener;
import com.common.android.utils.audio.AudioPlayer.onPlayStopListener;

import android.content.Context;

public class AudioAnimManager {
	public interface onPlayAudioAnimListener{
		void onPrepare();
		void onAnimPlaying(int current);
		void onPlayFinish();
	}
	private onPlayAudioAnimListener mAudioAnimListener;
	private AudioPlayer mAudioPlayer;
	private int mAnimActionCount=3;		//默认动画是3帧
	private int mCurrentAnimAction=0;	//当前播放的帧
	private boolean mbPlayingAudio=false;	//是否正在播放
	private File mPlayFile;
	private Context mContext;
	
	public AudioAnimManager(Context context){
		mContext=context;
		mAudioPlayer=AudioPlayer.getInstance();
	}
	
	/**
	 * @n<b>函数名称</b>     :setOnPlayFinishListener
	 * @brief 注册播放监听事件
	 * @version    Ver 1.3.3_modularization
	 * @param  @param listener
	 * @return void
	 * @<b>作者</b>          :  cb
	 * @<b>创建时间</b>      :  2014-2-8下午2:08:30      
	*/
	public void setOnPlayAudioAnimListener(onPlayAudioAnimListener listener){
		mAudioAnimListener=listener;
	}
	
	/**
	 * @n<b>函数名称</b>     :setAnimActionCount
	 * @brief 设置动画的帧数
	 * @version    Ver 1.3.3_modularization
	 * @param  @param count
	 * @return void
	 * @<b>作者</b>          :  cb
	 * @<b>创建时间</b>      :  2014-2-12下午6:06:42      
	*/
	public void setAnimActionCount(int count){
		mAnimActionCount=count;
	}
	
	/**
	 * @n<b>函数名称</b>     :stopPlay
	 * @brief 停止播放
	 * @version    Ver 1.3.3_modularization
	 * @param  
	 * @return void
	 * @<b>作者</b>          :  cb
	 * @<b>创建时间</b>      :  2014-2-12下午6:06:31      
	*/
	public void stopPlay(){
		if(mbPlayingAudio){
			mAudioPlayer.stopPlay();
		}
	}
	
	/**
	 * @n<b>函数名称</b>     :playAudio
	 * @brief 开始播放语音
	 * @version    Ver 1.3.3_modularization
	 * @param  @param file
	 * @param  @throws IllegalArgumentException
	 * @param  @throws IllegalStateException
	 * @param  @throws IOException
	 * @return void
	 * @<b>作者</b>          :  cb
	 * @<b>创建时间</b>      :  2014-2-12下午6:05:25      
	*/
	public void playAudio(File file) throws IllegalArgumentException, IllegalStateException, IOException{
		// 如果是同一个文件的话就是想暂停
		boolean bStop = ( mPlayFile != null && mPlayFile.equals(file) );
		//先停止上一个播放
		mAudioPlayer.stopPlay(true);
		if(bStop){
			return;
		}
		
		mPlayFile = file;
		mAudioPlayer.setOnPlayFinishListener(playFinishListener);
		mAudioPlayer.setOnPlayStopListener(playStopListener);
		//显示动画窗口
		if (mAudioAnimListener!=null){
			mAudioAnimListener.onPrepare();
		}
		mbPlayingAudio=true;
		mCurrentAnimAction=0;
		mAudioPlayer.play(mPlayFile, mContext);
		new Thread(new Runnable() {
			public void run() {
				while (mbPlayingAudio){
					try {
						if (mAudioAnimListener!=null){
							mAudioAnimListener.onAnimPlaying(mCurrentAnimAction);
						}
						Thread.sleep(1000);
						//开始显示动画，每1秒变化一次
						mCurrentAnimAction++;
						if (mCurrentAnimAction==mAnimActionCount){
							mCurrentAnimAction=0;
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}	
				}
			}
		}).start();
	}
	
	private onPlayFinishListener playFinishListener=new onPlayFinishListener() {
		@Override
		public void onFinish() {
			
			doFinish();
		}
	};
	
	private onPlayStopListener playStopListener = new onPlayStopListener() {
		
		@Override
		public void onStop() {
			// 目前，没有暂停，所以停止就相当于播放结束了
			doFinish();
		}
	};
	
	private void doFinish(){
		mbPlayingAudio = false;
		if (mAudioAnimListener!=null){
			mAudioAnimListener.onPlayFinish();
			mAudioPlayer.removeOnPlayFinishListener(playFinishListener);
			mAudioPlayer.removeOnPlayStopListener(playStopListener);
		}
		mPlayFile = null;
	}
}
